<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/6
 * Time: 17:16
 */

namespace App\Game;

use App\Game\Traits\SunBetTrait;
use App\Models\Def\MainGamePlat;
use App\Models\OddsHandicap;
use App\Models\QuotaHandicap;
use Carbon\Carbon;

class sunbet extends Game
{
    use SunBetTrait;

    const API_LOGIN = '/api/player/authorize';
    const API_KICK = '/api/player/deauthorize';
    const API_BALANCE = '/api/player/balance';
    const API_GAME = '/gamelauncher';
    const API_LOBBY_GAME_PC = '/sc88liveT5D';
    const API_LOBBY_GAME_MOBILE = '/sc88liveT5M';
    const API_DEBIT = '/api/wallet/debit';
    const API_CREDIT = '/api/wallet/credit';
    const API_HISTORY = '/api/report/bethistory';


    private $platName = 'sunbet';
    private $xSgsDate;


    public function __construct()
    {
        $this->xSgsDate = Carbon::now()->timezone('UTC')->format('Y-m-d\TH:m:s\Z');

        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'live',
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $token = $this->token($accountUserName, $request);

        if (!is_null($token)) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建用户失败'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName)
    {
        $params = [
            'userid' => $accountUserName,
            'cur' => 'RMB'
        ];

        $url = $this->gameUrl . self::API_BALANCE;
        $output = $this->sunBetRequest('GET', $params, $url, $this->header);
        $output = json_decode($output, true);
        if (array_key_exists('err', $output)) {
            if (is_null($output['err'])) {
                return ['success' => true, 'data' => ['balance' => $output['bal']], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['errdesc']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $json = [
            'userid' => $accountUserName,
            'amt' => $price,
            'cur' => 'RMB',
            'txid' => $transferid,
            'timestamp' => Carbon::now()->format('c'),
            'desc' => null

        ];
        $url = $this->gameUrl . self::API_CREDIT;
        $output = $this->sunBetRequest('POST', [], $url, $this->header, $json);
        $output = json_decode($output, true);
        if (array_key_exists('err', $output)) {
            if (is_null($output['err'])) {
                return ['success' => true, 'data' => ['transferid' => $output['ptxid'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['errdesc']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }


    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $json = [
            'userid' => $accountUserName,
            'amt' => $price,
            'cur' => 'RMB',
            'txid' => $transferid,
            'timestamp' => Carbon::now()->format('c'),
            'desc' => null

        ];
        $url = $this->gameUrl . self::API_DEBIT;
        $output = $this->sunBetRequest('POST', [], $url, $this->header, $json);
        $output = json_decode($output, true);
        if (array_key_exists('err', $output)) {
            if (is_null($output['err'])) {
                return ['success' => true, 'data' => ['transferid' => $output['ptxid'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['errdesc']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }


    //查询订单状态
    public function curlCheckTransfer($request)
    {
        return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '此平台没有次接口'];
    }


    //踢线
    public function curlKick($request)
    {
       /* $json = [
            'userid' => $request->get('accountUserName'),

        ];

        $url = $this->gameUrl . self::API_KICK;
        $output = $this->sunBetRequest('POST', [], $url, $this->header, $json);
        $output = json_decode($output, true);
        if (array_key_exists('err', $output)) {
            if (is_null($output['err'])) {
                return ['success' => true, 'data' => [], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '删除玩家会话失败，错误原因：' . $output['errdesc']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
        */
        return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        $token = $this->token($request->accountUserName, $request,0);
        if (!empty($token)) {
            if ($request->is_lobby) {
                $url = $this->joinGameUrl . self::API_LOBBY_GAME_PC . "?token=" . $token;
            } else {
                $url = $this->joinGameUrl . self::API_GAME . "?gpcode=" . $this->gameProvider . "&gcode=" . $request->gamecode . "&token=" . $token;
            }
            return ['success' => true, 'data' => ['url' => $url], 'message' => '成功'];

        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $token = $this->token($request->accountUserName, $request,1);
        if (!empty($token)) {
            if ($request->is_lobby) {
                $url = $this->joinGameUrl . self::API_LOBBY_GAME_MOBILE . "?token=" . $token;
            } else {
                $url = $this->joinGameUrl . self::API_GAME . "?gpcode=" . $this->gameProvider . "&gcode=" . $request->gamecode . "&token=" . $token;
            }
            return ['success' => true, 'data' => ['url' => $url], 'message' => '成功'];
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    /**
     * gameapi
     * 2018/12/6 22:15
     * Administrator
     * curlGetBetRecord
     * 游戏抓单
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $start_time = $startTime->format('c');
        $end_time = $endTime->format('c');
        $params = [
            'startdate' => $start_time,
            'enddate' => $end_time,
        ];
        $url = $this->gameUrl . self::API_HISTORY;
        $output = $this->sunBetRequest('GET', $params, $url, []);
        if (!empty($output)) {
            $data = $this->dataFormat($output);
            return $data;
        }


    }


}